package com.juc.syncup;

import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.TimeUnit;

/**
 * @Author : Zan
 * @Create : 2024/5/13 19:00
 * @ClassName : SynchronizedUpDemo
 * @Description : TODO 请用一句话描述该类的功能
 */
public class SynchronizedUpDemo {

    private static Object o = new Object();

    public static void main(String[] args) {

//        Object o = new Object();



    }

    private static void strongLock() {
        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "t2").start();
    }

    private static void biasedLock() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable()); // 虽然是偏向锁，但是因为没有线程ID，因此是无锁状态

        System.out.println("==================");

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "t1").start();
    }

    private static void noLock() {
        Object o = new Object();

        System.out.println("10进制：" + o.hashCode());
        System.out.println("16进制：" + Integer.toHexString(o.hashCode()));
        System.out.println("2进制：" + Integer.toBinaryString(o.hashCode()));

        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}
